package org.chen.wx.controller;

import org.chen.wx.utils.WxValidateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Author:Mr.Chen
 * Date:2018/8/3
 */
@RestController
@RequestMapping("validate")
public class WxValidateController {

    @Resource
    private WxValidateUtils wxValidateUtils;

    @GetMapping
    public String validateCode(HttpServletRequest request) {
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        if (wxValidateUtils.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        } else {
            return null;
        }
    }

}
